<template>
  <div>
    <!-- 面包屑导航 -->
    <Breadcrumb :breadcrumbInfo="breadcrumbInfo"></Breadcrumb>
    <el-card>
      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="15">
          <!-- 搜索框 -->
          <el-input
            v-model="queryInfo.query"
            placeholder="请输入内容"
            class="input-with-select"
            clearable
            @clear="searchOrder"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchOrder"
              >搜索</el-button
            >
          </el-input>
        </el-col>
      </el-row>
      <!-- 用户列表区 -->
      <el-table
        :data="OrderlistInfo.Orderlist.goods"
        stripe
        border
        highlight-current-row
      >
        <!-- 索引列 -->
        <el-table-column align="center" type="index"></el-table-column>

        <el-table-column align="center" prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column
          align="center"
          prop="order_price"
          label="订单价格(元)"
          width="120"
        >
        </el-table-column>
        <el-table-column
          align="center"
          width="100"
          prop="order_pay"
          label="是否付款"
          ><template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.order_pay === '0'"
              >未付款</el-tag
            >
            <el-tag v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="100"
          prop="is_send"
          label="是否发货"
        >
        </el-table-column>
        <el-table-column align="center" prop="create_time" label="下单时间">
          <!-- 时间格式化 -->
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <!-- 操作部分 -->
        <el-table-column label="操作" width="200px" align="center">
          <!-- 编辑 位置 按钮 -->
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="getEditOrder(scope.row)"
            ></el-button>
            <!-- 位置按钮 -->
            <el-button
              type="success"
              icon="el-icon-location-information"
              size="mini"
              @click="showProgressBox(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, OrderlistInfo.total]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="OrderlistInfo.total"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑Diaolog弹窗 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editOrderDialogInfo.isShow"
      width="50%"
      @close="editOrderDialogClosed"
    >
      <!-- editform表单 -->
      <el-form
        :model="editOrderDialogInfo.addressForm"
        :rules="editOrderRules"
        ref="editOrderRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <!-- 选择框 -->
          <el-cascader
            class="city-cascader"
            clearable
            :options="cityData"
            v-model="editOrderDialogInfo.address1"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editOrderDialogInfo.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editOrderDialogInfo.isShow = false">取 消</el-button>
        <el-button type="primary" @click="upEditOrder">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 时间线Diaolog弹窗 -->
    <el-dialog
      title="物流进度"
      :visible.sync="showProgressBoxDialogInfo.isShow"
      width="50%"
    >
      <span>这是一段信息</span>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from "../../components/Breadcrumb.vue";
import cityData from "./citydata.js";

export default {
  name: "Order",
  components: { Breadcrumb },
  data() {
    return {
      // 商品列表面包屑导航信息
      breadcrumbInfo: {
        titlePath: [
          { title: "首页", Path: "/Home" },
          { title: "订单管理", Path: "" },
          { title: "订单列表", Path: "" },
        ],
      },
      //  请求参数对象
      queryInfo: {
        query: "",
        //当前页数
        pagenum: 1,
        //当前页显示最大是数据
        pagesize: 5,
      },
      //   订单列表对象数组
      OrderlistInfo: {
        // 订单列表数组
        Orderlist: [],
        total: 0,
      },
      // 表单信息
      editOrderDialogInfo: {
        isShow: false,
        addressForm: {},
        address1: [],
        address2: "",
      },

      showProgressBoxDialogInfo: {
        isShow: false,
      },
      //   验证规则
      editOrderRules: {
        address1: [
          { required: true, message: "请选择省市区/县", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      //   城市数据
      cityData,
    };
  },
  methods: {
    //监听页码改变时会触发
    handleCurrentChange(newPagenum) {
      this.queryInfo.pagenum = newPagenum;
      //   从服务器获取数据
      this.getOrdersList();
    },
    //监听页面可以显示大小改变时会触发
    handleSizeChange(newPagesize) {
      this.queryInfo.pagesize = newPagesize;
      //   从服务器获取数据
      this.getOrdersList();
    },

    //   获取订单列表
    async getOrdersList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        this.$message.error("获取订单列表失败!");
      }

      this.OrderlistInfo.Orderlist = res.data;
      this.OrderlistInfo.total = res.data.total;
    },
    //搜索订单
    searchOrder() {
      this.getOrdersList();
    },
    // 编辑订单
    getEditOrder(row) {
      this.editOrderDialogInfo.isShow = true;
    },
    upEditOrder() {
      //表单初步验证
      if (
        this.editOrderDialogInfo.address2.trim() === "" ||
        this.editOrderDialogInfo.address1.length === 0
      ) {
        return this.$message.error("请填写必要选项!");
      }
      this.editOrderDialogInfo.isShow = false;
    },
    //监听编辑对话框的关闭
    editOrderDialogClosed() {
      this.editOrderDialogInfo.address1 = [];
      this.editOrderDialogInfo.address2 = "";
      this.$refs.editOrderRef.resetFields();
    },
    // 显示进程
    async showProgressBox(id) {
      //   const { data: res } = await this.$http.get("/kuaidi/462006046255802");
      //   if (res.meta.status !== 200) {
      //     return this.$message.error("获取物流信息失败！!");
      //   }
      //   this.showProgressBoxDialogInfo.isShow = true;
      return this.$message.error("获取物流接口已经失效！!");
    },
  },

  created() {
    this.getOrdersList();
  },
};
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 10px;
}
.el-pagination {
  margin-top: 15px;
}
.city-cascader {
  width: 300px;
}
</style>